<template>
    <ContentWrap>
        <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="70px" @submit.native.prevent>
            <el-form-item label="归属学校" prop="schoolId" v-if="tenantId == 1">
                <el-select v-model="queryParams.schoolId" placeholder="请选择" class="!w-240px" filterable default-first-option clearable>
                    <el-option v-for="item in schools" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select>
            </el-form-item>
            <el-form-item label="公告标题" prop="title">
                <el-input v-model="queryParams.title" placeholder="请输入公告标题" clearable @keyup.enter="handleQuery" class="!w-240px"/>
            </el-form-item>
            <el-form-item label="公告状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择公告状态" clearable class="!w-240px">
                    <el-option v-for="dict in getIntDictOptions(DICT_TYPE.YXT_BANNER_STATUS)" :key="dict.value" :label="dict.label" :value="dict.value"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button @click="handleQuery" v-hasPermi="['yxt:notify:query']">
                    <Icon icon="ep:search" class="mr-5px"/>
                    搜索
                </el-button>
                <el-button @click="resetQuery">
                    <Icon icon="ep:refresh" class="mr-5px"/>
                    重置
                </el-button>
                <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['yxt:notify:create']">
                    <Icon icon="ep:plus" class="mr-5px"/>
                    新增
                </el-button>
            </el-form-item>
        </el-form>
    </ContentWrap>

    <ContentWrap>
        <el-table v-loading="loading" :data="list">
            <el-table-column label="公告标题" align="center" prop="title" min-width="120"/>
            <el-table-column label="推送分类" align="center" prop="type" min-width="300">
                <template #default="scope">
                    <dict-tag :type="DICT_TYPE.YXT_NOTIFY_PUSH_TYPE" :value="scope.row.type"/>
                </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status">
                <template #default="scope">
                    <dict-tag :type="DICT_TYPE.YXT_BANNER_STATUS" :value="scope.row.status"/>
                </template>
            </el-table-column>
            <el-table-column label="推送时间" align="center" prop="createTime" width="180" :formatter="dateFormatter"/>
            <el-table-column label="操作" align="center" width="200">
                <template #default="scope">
                    <el-button link type="primary" @click="openForm('update', scope.row.id)" v-hasPermi="['yxt:notify:update']">
                        编辑
                    </el-button>
                    <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['yxt:notify:delete']">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="getList"/>
    </ContentWrap>

    <NotifyForm ref="formRef" @success="getList"/>
</template>
<script lang="ts" setup>
import {DICT_TYPE, getIntDictOptions} from '@/utils/dict'
import {dateFormatter} from '@/utils/formatTime'
import {useNotifyApi} from "@/api/yxt/notify";
import NotifyForm from "@/views/yxt/notify/NotifyForm.vue";
import {useConfirm, useMessage} from "@/components/Notice";
import {getTenantId} from "@/utils/auth";
import {useSchoolApi} from "@/api/yxt/school";

defineOptions({name: 'YxtNotify'})

const tenantId = getTenantId();
const schoolApi = useSchoolApi();

const {t} = useI18n()
const loading = ref<boolean>(true)
const total = ref<number>(0)
const list = ref<Array<any>>([])
const queryParams = reactive({
    pageNo: 1,
    pageSize: 10,
    schoolId: '',
    title: '',
    type: undefined,
    status: undefined
})
const queryFormRef = ref()
const schools = ref<Array<any>>([])

const getList = async () => {
    loading.value = true
    try {
        const data = await useNotifyApi().getNotifyPage(queryParams)
        list.value = data.list
        total.value = data.total
    } finally {
        loading.value = false
    }
}

const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
}

const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
}

const formRef = ref()
const openForm = (type: string, id?: number) => {
    formRef.value.open(type, id)
}

const handleDelete = (id: number) => {
    useConfirm(t("common.delMessage"), async () => {
        await useNotifyApi().deleteNotify(id)
        getList().finally(() => useMessage(t('common.delSuccess')));
    });
}

onMounted(async () => {
    await getList();
    schools.value = await schoolApi.getSimpleList();
})
</script>
